from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from lib.django import models as mymodels
from .project import Project
from lib import constants

from .lib import ProjectSubMixin

class Material(ProjectSubMixin, models.Model):
    project = models.ForeignKey(
        verbose_name=_("项目"),
        to=Project,
        on_delete=models.CASCADE,
    )

    name = models.CharField(
        verbose_name=_("物资器材名称"), 
        max_length=constants.TITLE_FIELD_LENGTH,
    )

    quantity = models.PositiveIntegerField(
        verbose_name=_("数量(个/件)"),
        default=0,
        blank=True
    )

    received_date = models.DateField(
        verbose_name=_("接收日期"),
        null=True,
        blank=True
    )

    requires_return = mymodels.YesNoField(
        verbose_name=_("是否需要返还"),
        default=False,
        blank=True
    )

    return_date = models.DateField(
        verbose_name=_("返还日期"),
        null=True,
        blank=True
    )

    return_note = models.TextField(
        verbose_name=_("未返回说明"),
        blank=True,
        default=""
    )

    returned_quantity = models.PositiveIntegerField(
        verbose_name=_("返还数量"),
        default=0,
        blank=True
    )

    class Meta:
        default_permissions = []
        db_table = "project_material"
        verbose_name = _("项目物资")
        verbose_name_plural = verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:material/detail", kwargs={'project_pk': project_id, 'pk': id})

